function permute(nums: number[]): number[][] {
  const n = nums.length;
  const ans: number[][] = [];
  const used: boolean[] = [];
  const u: number[] = [];
  recur(0);
  return ans;
  function recur(pos: number) {
    if (pos === n) {
      ans.push([...u]);
      return;
    }
    for (let i = 0; i < n; i++) {
      if (!used[i]) {
        u.push(nums[i]);
        used[i] = true;
        recur(pos + 1);
        used[i] = false;
        u.pop();
      }
    }
  }
};